// RunStates.ts (文件名已变更)
import { ICharacterState } from './CharacterState';
import { PlayerController } from '../PlayerController';
import { CharacterState } from '../../enum';
import { Vec2 } from 'cc';

// 类名同步修改为 RunStates
export class RunStates implements ICharacterState {
    enter(controller: PlayerController) {
        controller.playerAnim.crossFade('run', 0.2);
    }

    update(controller: PlayerController, deltaTime: number) {
        // 修复方向获取方式
        const direction = controller.joystick.getDirection();
    
    // 修复位置更新方式
    const currentPos = controller.node.position;
    controller.node.setPosition(
        currentPos.x + direction.x * controller.moveSpeed * deltaTime,
        currentPos.y + direction.y * controller.moveSpeed * deltaTime
    );

    if (direction.equals(Vec2.ZERO)) {
        controller.transitionTo(CharacterState.STAND);
    }
  // 修改方向判断逻辑
    controller.node.setScale(direction.x !== 0 ? Math.sign(direction.x) : controller.node.scale.x, 1);
    }

    exit(controller: PlayerController) {}
}